package com.sdry.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.LineSideLibrary;
import com.sdry.model.hpf.Part;
import com.sdry.model.jyy.Shedudetail;
import com.sdry.model.jyy.ShedudetailExample;
import com.sdry.model.lz.DeviceBreakdown;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.CentrifugalExample;
import com.sdry.model.workplace.CentrifugalExample.Criteria;
import com.sdry.model.workplace.WorkCentrifugalDetails;
import com.sdry.service.hpf.PartService;
import com.sdry.service.jyy.ShedudetailServise;
import com.sdry.service.report.ReportServise;
import com.sdry.service.workplace.CentrifugalServise;
import com.sdry.service.workplace.OthersService;
import com.sdry.service.workplace.WorkCentrifugalDetailsService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;
import com.sdry.web.controller.workplace.RunStartTwo;

/**
 * 
 * @ClassName:JumpPageController
 * @Description:看板
 * @Author tdd
 * @Date 2018年4月25日下午5:05:43
 * @version 1.0
 */
@Controller
@RequestMapping("/Kanban")
public class KanbanController {
	/**
	 * 离心机现况版跳转页面
	 * @return
	 */
	@RequestMapping("/lxj")
	public String lxj() {
		return "/Kanban/lxj";
	}
	 @Resource
	 private CentrifugalServise centrifugalServise;
	 /**
     * 根据当前日期查询离心信息
     * @param response 响应
     * @param format 日期
     * @throws Exception 异常
     */
    @RequestMapping("/selectNumByDate")
    public void selectNumByDate(HttpServletResponse response,String format) throws Exception {
    	CentrifugalExample example = new CentrifugalExample();
    	Criteria createCriteria =  example.createCriteria();
    	//createCriteria.andCtimeLike("%"+format+"%");
    	createCriteria.andCremark5Like("%"+format+"%");
    	List<Centrifugal> centrifugals = centrifugalServise.selectByExample(example);
    	ResponseUtil.write(response, centrifugals.size());
    }
	
	@Resource
	private ShedudetailServise shedudetailServise;
	
	/**
	 * 根据 产品 和 工位 查询排产单
	 * @param product 产品
	 * @param position 工位
	 * @param response 响应
	 * @throws Exception  异常
	 */
	@RequestMapping("/selectByDate")
	public void selectByDate(String format,String product,String position,HttpServletResponse response) throws Exception {
		
		String num = "";
		int num2= 0;
		ShedudetailExample example = new ShedudetailExample();
		com.sdry.model.jyy.ShedudetailExample.Criteria createCriteria = example.createCriteria();
		
		createCriteria.andRemark1EqualTo(format);
		//根据时间查询排产单
		List<Shedudetail> list = shedudetailServise.selectByExample(example );
		
		if(list.size() >0) {
			if("膜丝".equals(position)) {
				if("PSH2-0960-T-01".equals(product)) {
					//b1
					num = list.get(0).getB1();
				}else if("PSH2-0980-T-02".equals(product)) {
					//c1
					num = list.get(0).getC1();
				}else if("PSH1-1080-C-04".equals(product)) {
					//d1
					num = list.get(0).getD1();
				}else if("PSH1-1060-B-03".equals(product)) {
					//e1
					num = list.get(0).getE1();
				}else if("".equals(product) || null == product) {
					int count = StringUtil.strNum(list.get(0).getB1())
							+StringUtil.strNum(list.get(0).getC1())
							+StringUtil.strNum(list.get(0).getD1())
							+StringUtil.strNum(list.get(0).getE1());
					num = String.valueOf(count);
				}
				
			}else if("组装".equals(position)) {
				if("PSH2-0960-T-01".equals(product)) {
					//b4
					num = list.get(0).getB4();
				}else if("PSH2-0980-T-02".equals(product)) {
					//c4
					num = list.get(0).getC4();
				}else if("PSH1-1080-C-04".equals(product)) {
					//d4
					num = list.get(0).getD4();
				}else if("PSH1-1060-B-03".equals(product)) {
					//e4
					num = list.get(0).getE4();
				}else if("".equals(product) || null == product) {
					int count = StringUtil.strNum(list.get(0).getB4())
							+StringUtil.strNum(list.get(0).getC4())
							+StringUtil.strNum(list.get(0).getD4())
							+StringUtil.strNum(list.get(0).getE4());
					num = String.valueOf(count);
				}
			}else if("离心".equals(position)) {
				if("PSH2-0960-T-01".equals(product)) {
					//b7
					num = list.get(0).getB7();
				}else if("PSH2-0980-T-02".equals(product)) {
					//c7
					num = list.get(0).getC7();
				}else if("PSH1-1080-C-04".equals(product)) {
					//d7
					num = list.get(0).getD7();
				}else if("PSH1-1060-B-03".equals(product)) {
					//e7
					num = list.get(0).getE7();
				}else if("".equals(product) || null == product) {
					int count = StringUtil.strNum(list.get(0).getB7())
							+StringUtil.strNum(list.get(0).getC7())
							+StringUtil.strNum(list.get(0).getD7())
							+StringUtil.strNum(list.get(0).getE7());
					num = String.valueOf(count);
				}
			}else if("固化".equals(position)) {
				if("PSH2-0960-T-01".equals(product)) {
					//b9
					num = list.get(0).getB9();
				}else if("PSH2-0980-T-02".equals(product)) {
					//c9
					num = list.get(0).getC9();
				}else if("PSH1-1080-C-04".equals(product)) {
					//d9
					num = list.get(0).getD9();
				}else if("PSH1-1060-B-03".equals(product)) {
					//e9
					num = list.get(0).getE9();
				}else if("".equals(product) || null == product) {
					int count = StringUtil.strNum(list.get(0).getB9())
							+StringUtil.strNum(list.get(0).getC9())
							+StringUtil.strNum(list.get(0).getD9())
							+StringUtil.strNum(list.get(0).getE9());
					num = String.valueOf(count);
				}
			}else if("切割".endsWith(position)) {
				if("PSH2-0960-T-01".equals(product)) {
					//b10
					num = list.get(0).getB10();
				}else if("PSH2-0980-T-02".equals(product)) {
					//c10
					num = list.get(0).getC10();
				}else if("PSH1-1080-C-04".equals(product)) {
					//d10
					num = list.get(0).getD10();
				}else if("PSH1-1060-B-03".equals(product)) {
					//e10
					num = list.get(0).getE10();
				}else if("".equals(product) || null == product) {
					int count = StringUtil.strNum(list.get(0).getB10())
							+StringUtil.strNum(list.get(0).getC10())
							+StringUtil.strNum(list.get(0).getD10())
							+StringUtil.strNum(list.get(0).getE10());
					num = String.valueOf(count);
				}
			}
			
			if(null == num || "".equals(num)) {
				ResponseUtil.write(response, num2);
			}else {
				ResponseUtil.write(response, num);
			}
		}else {
			ResponseUtil.write(response, num2);
		}
	}
	
	
	@Resource WorkCentrifugalDetailsService workCentrifugalDetailsService;
    /**
     * 根据当前日期及离心编号查询离心时间
     * @param response 响应
     * @param format 日期
     * @throws Exception 异常
     */
    @RequestMapping("/selectTimeByCcode")
    public void selectTimeByCcode(HttpServletResponse response
    		,String ccode) throws Exception {
    	CentrifugalExample example = new CentrifugalExample();
    	Criteria createCriteria =  example.createCriteria();
    	//离心机编号
    	createCriteria.andCcodeEqualTo(ccode);
    	//结束时间
    	createCriteria.andCremarkIsNull();
    	//是否报废   1是   0否
        createCriteria.andCremark1EqualTo("0");
    	List<Centrifugal> centrifugals = centrifugalServise.selectByExample(example);
    	WorkCentrifugalDetails centrifugalDetails = new WorkCentrifugalDetails();
    	centrifugalDetails.setCid(String.valueOf(centrifugals.get(0).getCid()));
		List<WorkCentrifugalDetails> centrifugalDetailsList = workCentrifugalDetailsService.queryAllByMution(centrifugalDetails);
    	String time = "0分";
    	RunStartTwo startTwo = new RunStartTwo();
    	for (int i = 0; i < centrifugalDetailsList.size()-1; i++) {
    		time = startTwo.addTime(centrifugalDetailsList.get(i).getCdremark3(), time);
		}
    	//最后一条
    	centrifugalDetails = centrifugalDetailsList.get(centrifugalDetailsList.size() - 1);
    	
    	String cdremark2 = centrifugalDetails.getCdremark2();
    	SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String date = sDateFormat.format(new Date());
    	// 未临时停止
		if ("".equals(cdremark2) || null == cdremark2) {
			// 开始时间
			String cdremark = centrifugalDetails.getCdremark();
			// 计算开始时间到此刻的时间差
			String timeDifference = RunStartTwo.getTimeDifference(date,
					cdremark);
			time = startTwo.addTime(timeDifference, time);
		//临时停止中
		} else if ("-1".equals(cdremark2)) {
		//临时开始时间
		}else{
			// 计算临时开始时间到此刻的时间差
			String timeDifference = RunStartTwo.getTimeDifference(date,
					cdremark2);
			//胶注时间 = 胶注时间+时间差
			timeDifference = startTwo.addTime(timeDifference, centrifugalDetails.getCdremark3());
			time = startTwo.addTime(timeDifference, time);
		}
		time = minZhuanHour(time);
		JSONObject jsonObject=new JSONObject();
        jsonObject.put("wenDu", centrifugalDetails.getCdremark4());
        jsonObject.put("time",time);
        List<String> ctubes = new ArrayList<>();
        for (int i = 0; i < centrifugals.size(); i++) {
        	ctubes.add(centrifugals.get(i).getCtube());
		}
        jsonObject.put("ctubes",ctubes);
        ResponseUtil.write(response, jsonObject);
    	//ResponseUtil.write(response,time);
    }
    /***
	 * 时间差求和
	 * 
	 * @param time
	 *            ...分
	 */
	public static String minZhuanHour(String time) {
		// 格式日期格式，在此我用的是"2018-01-24 19:49:50"这种格式
		// 可以更改为自己使用的格式，例如：yyyy/MM/dd HH:mm:ss 。。。
		try {
			time = time.replace("分", "");
			long min = Long.valueOf(time);
			long hour = (min / (60));
			min = (min - hour * 60);
			if(min > 30){
				hour = hour++;
			}
			return "" + hour + "h";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    /**
	 * 组装生产线跳转页面
	 * @return
	 */
	@RequestMapping("/assemble")
	public String assemble() {
		return "/Kanban/assemble";
	}
	
	@Resource OthersService othersService;
	/**
	 * 查询当前日期组装个数
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/assembleNumByDate")
	public void assembleNumByDate(HttpServletResponse response
			,Assemble assemble,String product) throws Exception {
		Map<String, Object> map = new HashMap<>();
		if (assemble.getAtube() != null) {
			map.put("atube", assemble.getAtube());
		} else {
			map.put("atube", "");
		}
		if (assemble.getAtime() != null) {
			map.put("atime", assemble.getAtime());
		} else {
			map.put("atime", "");
		}
		if (assemble.getAremark5() != null) {
			map.put("aremark5", assemble.getAremark5());
		} else {
			map.put("aremark5", "");
		}
		if (product != null) {
			map.put("product", product);
		} else {
			map.put("product", "");
		}
		//是否删除
		map.put("aremark", "0");
		
		List<Assemble> assembles = othersService.selectAtubeByDateAndProduct(map);
		ResponseUtil.write(response,assembles.size());
	}
	@Resource ReportServise reportServise; 
	/**
	 * 查询设备报修
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/deviceBreakdownByDeviceName")
	public void deviceBreakdownByDeviceName(HttpServletResponse response
			,String deviceName,String repairTime) throws Exception{
		List<String> list = new ArrayList<>();
		DeviceBreakdown record = new DeviceBreakdown();
		record.setDeviceName(deviceName);
		record.setRepairTime(repairTime);
		List<DeviceBreakdown> deviceBreakdowns = reportServise.deviceBreakdownByMution(record);
		list.add(String.valueOf(deviceBreakdowns.size()));
		String ss = "正常";
		if(deviceBreakdowns.size()>0 && "0".equals(deviceBreakdowns.get(deviceBreakdowns.size() - 1).getDeviceStatus())){
			ss = "报修";
		}
			
		list.add(ss);
		//将对象转化为json数组
        JSONArray json = JSONArray.fromObject(list);
        ResponseUtil.write(response, json);
	}
	@Resource PartService partService;
	/**
	 * 查询设备报修
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/lineSideLibraryNumByPaname")
	public void lineSideLibraryNumByPaname(HttpServletResponse response
			,String paname) throws Exception{
		LineSideLibrary record = new LineSideLibrary();
		record.setPaname(paname);
		List<LineSideLibrary> lineSideLibrarys = reportServise.lineSideLibraryByMution(record);
		String num = "0";
		for (int i = 0; i < lineSideLibrarys.size(); i++) {
			num = StringUtil.calcFloatValue(num, lineSideLibrarys.get(i).getQuantity(), "add");
		}
		String dw = "";
		if(lineSideLibrarys.size() > 0){
			dw = lineSideLibrarys.get(0).getPaunit();
		}else{
			Map<String, Object> map = new HashMap<>();
			map.put("limit", 1);
			map.put("page", 1);
			map.put("paname", paname);
			List<Part> partList = partService.listPagePart(map);
			if(partList.size()>0){
				dw = partList.get(0).getParemark1();
			}
		}
		ResponseUtil.write(response,num+dw);
	}
	
	/**
     * 根据当前日期及离心编号查询对应膜管编号
     * @param response 响应
     * @param format 日期
     * @throws Exception 异常
     */
    @RequestMapping("/selectCtubeByCcode")
    public void selectCtubeByCcode(HttpServletResponse response
    		,String ccode) throws Exception {
    	CentrifugalExample example = new CentrifugalExample();
    	Criteria createCriteria =  example.createCriteria();
    	//离心机编号
    	createCriteria.andCcodeEqualTo(ccode);
    	//结束时间
    	createCriteria.andCremarkIsNull();
    	//是否报废   1是   0否
        createCriteria.andCremark1EqualTo("0");
    	List<Centrifugal> centrifugals = centrifugalServise.selectByExample(example);
		JSONObject jsonObject=new JSONObject();
        List<String> ctubes = new ArrayList<>();
        for (int i = 0; i < centrifugals.size(); i++) {
        	ctubes.add(centrifugals.get(i).getCtube());
		}
        jsonObject.put("ctubes",ctubes);
        ResponseUtil.write(response, jsonObject);
    	//ResponseUtil.write(response,time);
    }
}
